<?php
if (!(defined('IN_IA'))) 
{
	exit('Access Denied');
}
class Route_Model
{
	public function run($isweb = true) 
	{
		global $_GPC;
		global $_W;
		require_once IA_ROOT . '/addons/'.MODEL_NAME.'/core/inc/page.php';
		if ($isweb) 
		{
			require_once MODEL_CORE . 'inc/page_web.php';
			require_once MODEL_CORE . 'inc/page_web_com.php';
		}
		$r = str_replace('//', '/', trim($_GPC['r'], '/'));
		$routes = explode('.', $r);
		$segs = count($routes);
		$method = 'main';
		$root = MODEL_CORE_WEB;
		if ($routes[0] == 'system')
		{
			require_once MODEL_CORE . 'inc/page_system.php';
		}
		switch ($segs) 
		{
			case 0: $file = $root . 'index.php';
			$class = 'Index';
			case 1: $file = $root . $routes[0] . '.php';
			if (is_file($file)) 
			{
				$class = ucfirst($routes[0]);
			}
			else if (is_dir($root . $routes[0])) 
			{
				$file = $root . $routes[0] . '/index.php';
				$class = 'Index';
			}
			else 
			{
				$method = $routes[0];
				$file = $root . 'index.php';
				$class = 'Index';
			}
			$_W['action'] = $routes[0];
			break;
			case 2: $_W['action'] = $routes[0] . '.' . $routes[1];
			$file = $root . $routes[0] . '/' . $routes[1] . '.php';
			if (is_file($file)) 
			{
				$class = ucfirst($routes[1]);
			}
			else if (is_dir($root . $routes[0] . '/' . $routes[1])) 
			{
				$file = $root . $routes[0] . '/' . $routes[1] . '/index.php';
				$class = 'Index';
			}
			else 
			{
				$file = $root . $routes[0] . '.php';
				if (is_file($file)) 
				{
					$method = $routes[1];
					$class = ucfirst($routes[0]);
				}
				else if (is_dir($root . $routes[0])) 
				{
					$method = $routes[1];
					$file = $root . $routes[0] . '/index.php';
					$class = 'Index';
				}
				else 
				{
					$file = $root . 'index.php';
					$class = 'Index';
				}
			}
			$_W['action'] = $routes[0] . '.' . $routes[1];
			break;
			case 3: $_W['action'] = $routes[0] . '.' . $routes[1] . '.' . $routes[2];
			$file = $root . $routes[0] . '/' . $routes[1] . '/' . $routes[2] . '.php';
			if (is_file($file)) 
			{
				$class = ucfirst($routes[2]);
			}
			else if (is_dir($root . $routes[0] . '/' . $routes[1] . '/' . $routes[2])) 
			{
				$file = $root . $routes[0] . '/' . $routes[1] . '/' . $routes[2] . '/index.php';
				$class = 'Index';
			}
			else 
			{
				$method = $routes[2];
				$file = $root . $routes[0] . '/' . $routes[1] . '.php';
				if (is_file($file)) 
				{
					$class = ucfirst($routes[1]);
				}
				else if (is_dir($root . $routes[0] . '/' . $routes[1])) 
				{
					$file = $root . $routes[0] . '/' . $routes[1] . '/index.php';
					$class = 'Index';
				}
				$_W['action'] = $routes[0] . '.' . $routes[1];
			}
			break;
			case 4: $_W['action'] = $routes[0] . '.' . $routes[1] . '.' . $routes[2];
			$method = $routes[3];
			$class = ucfirst($routes[2]);
			$file = $root . $routes[0] . '/' . $routes[1] . '/' . $routes[2] . '.php';
			break;
		}
		if (!(is_file($file))) 
		{
			show_message('未找到控制器 ' . $r);
		}
		$_W['routes'] = $r;
		$_W['controller'] = $routes[0];
		include $file;
		$class = ucfirst($class) . '_Page';
		$instance = new $class();
		if (!(method_exists($instance, $method))) 
		{
			show_message('控制器 ' . $_W['controller'] . ' 方法 ' . $method . ' 未找到!');
		}
		$instance->$method();
		exit();
	}

    public function api()
    {

        global $_GPC;
        global $_W;
        $r = str_replace('//', '/', trim($_GPC['r'], '/'));
        $routes = explode('.', $r);
        $segs = count($routes);
        $method = 'main';
        $root = MODEL_CORE_API;

        switch ($segs)
        {
            case 0: $file = $root . 'index.php';
                $class = 'Index';
            case 1: $file = $root . $routes[0] . '.php';
                if (is_file($file))
                {
                    $class = ucfirst($routes[0]);
                }
                else if (is_dir($root . $routes[0]))
                {
                    $file = $root . $routes[0] . '/index.php';
                    $class = 'Index';
                }
                else
                {
                    $method = $routes[0];
                    $file = $root . 'index.php';
                    $class = 'Index';
                }
                $_W['action'] = $routes[0];
                break;
            case 2: $_W['action'] = $routes[0] . '.' . $routes[1];

                $file = $root . $routes[0] . '/' . $routes[1] . '.php';

                if (is_file($file))
                {
                    $class = ucfirst($routes[1]);

                }
                else if (is_dir($root . $routes[0] . '/' . $routes[1]))
                {
                    $file = $root . $routes[0] . '/' . $routes[1] . '/index.php';
                    $class = 'Index';
                }
                else
                {
                    $file = $root . $routes[0] . '.php';
                    if (is_file($file))
                    {
                        $method = $routes[1];
                        $class = ucfirst($routes[0]);
                    }
                    else if (is_dir($root . $routes[0]))
                    {
                        $method = $routes[1];
                        $file = $root . $routes[0] . '/index.php';
                        $class = 'Index';
                    }
                    else
                    {
                        $file = $root . 'index.php';
                        $class = 'Index';
                    }
                }
                $_W['action'] = $routes[0] . '.' . $routes[1];
                break;
            case 3: $_W['action'] = $routes[0] . '.' . $routes[1] . '.' . $routes[2];
                $file = $root . $routes[0] . '/' . $routes[1] . '/' . $routes[2] . '.php';
                if (is_file($file))
                {
                    $class = ucfirst($routes[2]);
                }
                else if (is_dir($root . $routes[0] . '/' . $routes[1] . '/' . $routes[2]))
                {
                    $file = $root . $routes[0] . '/' . $routes[1] . '/' . $routes[2] . '/index.php';
                    $class = 'Index';
                }
                else
                {
                    $method = $routes[2];
                    $file = $root . $routes[0] . '/' . $routes[1] . '.php';
                    if (is_file($file))
                    {
                        $class = ucfirst($routes[1]);
                    }
                    else if (is_dir($root . $routes[0] . '/' . $routes[1]))
                    {
                        $file = $root . $routes[0] . '/' . $routes[1] . '/index.php';
                        $class = 'Index';
                    }
                    $_W['action'] = $routes[0] . '.' . $routes[1];
                }
                break;
            case 4: $_W['action'] = $routes[0] . '.' . $routes[1] . '.' . $routes[2];
                $method = $routes[3];
                $class = ucfirst($routes[2]);
                $file = $root . $routes[0] . '/' . $routes[1] . '/' . $routes[2] . '.php';
                break;
        }
        if (!(is_file($file)))
        {
            echo json_encode(['code'=>'400','message' => '未找到控制器'.$r,'data'=>'']);
            exit;
        }
        $_W['routes'] = $r;
        $_W['controller'] = $routes[0];
        include $file;
        $class = ucfirst($class);
        $instance = new $class();
        if (!(method_exists($instance, $method)))
        {
            echo json_encode(['code'=>'400','message' => '控制器 ' . $_W['controller'] . ' 方法 ' . $method . ' 未找到!','data'=>'']);
            exit;
        }
        $instance->$method();
        exit();
    }
}
?>